package com.chuangke.mds.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuangke.common.utils.FileUtils;
import com.chuangke.mds.constant.ProcessType;
import com.chuangke.mds.dao.TechnologyProcessProcedureDao;
import com.chuangke.mds.entity.TechnologyProcessProcedure;
import com.chuangke.mds.service.TechnologyProcessParamsService;
import com.chuangke.mds.service.TechnologyProcessProcedureNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Throwable.class)
public class TechnologyProcessParamsServiceImpl  implements TechnologyProcessParamsService {
    @Autowired
    private TechnologyProcessProcedureNodeService processProcedureService;
    @Autowired
    private TechnologyProcessProcedureDao nodeDao;
    @Override
    public List<Map<String, Object>> getProcessParams(String type) {
        String file = FileUtils.getFileStr("classpath:form/mds/process_parameters.json");
        Map<String,List> map = JSON.parseObject(file, Map.class);
        return map.get(type);
    }

    @Override
    public Map<String, Object> getSimulationParams(String processProcedureId) {
        Map<String,Object> resultMap=new HashMap<>();
        List<TechnologyProcessProcedure> nodeList =nodeDao.list(new QueryWrapper<>(new TechnologyProcessProcedure()).lambda().eq(TechnologyProcessProcedure::getId, processProcedureId));
        if(nodeList==null||nodeList.isEmpty()){
            return resultMap;
        }
        //只有一个虚拟节点
        TechnologyProcessProcedure node = nodeList.get(0);
        String type=node.getType();
        //仿真参数信息
        String file = FileUtils.getFileStr("classpath:form/mds/simulation_parameters.json");
        Map<String,List> map =JSON.parseObject(file, Map.class);
        resultMap.put("columns",map.get(type));
        //工艺参数信息
        String gyFile = FileUtils.getFileStr("classpath:form/mds/process_parameters.json");
        Map<String,List> gyMap =JSON.parseObject(gyFile, Map.class);
        resultMap.put("gyColumns",gyMap.get(type));

        String nodeInfo = node.getProcedureData();
        Map<String,Object> valueMap = JSON.parseObject(nodeInfo, Map.class);
        String paramConf=valueMap.get("tableData").toString();

        resultMap.put("procedureData",valueMap);
        List<Map<String,Object>> paramConfList = JSON.parseObject(paramConf, new TypeReference<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
        List<Map<String,Object>> simulationDataList=getSimulationDatas(type,paramConfList);
        resultMap.put("datas",simulationDataList);
        return resultMap;
    }
    @Override
    public Map<String, Object> getSimulationParams(Map<String, Object> params) {
        String processProcedureId =  params.get("processProcedureId").toString();
        String type = params.get("type").toString();
        if(type.contains("RCL")){
            type="RCL";
        }else if(type.contains("SKJC")){
            type="SKJC";
        }else if (type.contains("GYLX")){
            type="GYLX";
        }
        Map<String,Object> resultMap=new HashMap<>();
        List<TechnologyProcessProcedure> nodeList =nodeDao.list(new QueryWrapper<>(new TechnologyProcessProcedure()).lambda().eq(TechnologyProcessProcedure::getId, processProcedureId));
        if(nodeList==null||nodeList.isEmpty()){
            return resultMap;
        }
        //只有一个虚拟节点
        TechnologyProcessProcedure node = nodeList.get(0);
        String nodeType=node.getType();
        //仿真参数信息
        String file = FileUtils.getFileStr("classpath:form/mds/simulation_parameters.json");
        Map<String,List> map =JSON.parseObject(file, Map.class);
        resultMap.put("columns",map.get(type));
        //工艺参数信息
        String gyFile = FileUtils.getFileStr("classpath:form/mds/process_parameters.json");
        Map<String,List> gyMap =JSON.parseObject(gyFile, Map.class);
        resultMap.put("gyColumns",gyMap.get(nodeType));

        String nodeInfo = node.getProcedureData();
        Map<String,Object> valueMap = JSON.parseObject(nodeInfo, Map.class);
        String paramConf=valueMap.get("tableData").toString();

        resultMap.put("procedureData",valueMap);
        List<Map<String,Object>> paramConfList = JSON.parseObject(paramConf, new TypeReference<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> parseObject(String text) {
                return super.parseObject(text);
            }
        });
        List<Map<String,Object>> simulationDataList=getSimulationDatas(type,paramConfList);
        resultMap.put("datas",simulationDataList);
        return resultMap;
    }

    /**
     * 根据工艺参数数据生成仿真数据
     * @param gyDataList
     * @return
     */
    private static List<Map<String, Object>> getSimulationDatas(String type,List<Map<String, Object>> gyDataList) {
        if(gyDataList==null||gyDataList.isEmpty()){
            return new ArrayList<>();
        }
        //热处理和数控机床的仿真参数与工艺参数完全相同
        if(ProcessType.RCL.name().equals(type)||ProcessType.SKJC.name().equals(type)||ProcessType.GYLX.name().equals(type)){
            return gyDataList;
        }
        List<Map<String, Object>> simulationDataList=new ArrayList<>();
        for (Map<String, Object> gyData : gyDataList) {
            Map<String, Object> simulationData=new HashMap<>();
            //公式=工件半径*3.14/单元格数量/60/切削速度
            simulationData.put("scsc",calcScsj(gyData.get("gjbj"),gyData.get("qxsd")));
            simulationData.put("dygsl",100);
            simulationData.put("djhd",gyData.get("qxshd"));
            simulationData.put("srxs",2000);
            simulationData.put("jsd", calcJsd(gyData.get("qxsd"),gyData.get("mxbj")));
            simulationData.put("qxsd",gyData.get("qxshd"));
            simulationData.put("lqmc",100);
            simulationData.put("jgsd", calcJjsd(gyData.get("jjl"),gyData.get("qxsd"),gyData.get("mxbj")));
            simulationData.put("jgsdX", calcJjsd(gyData.get("jjlX"),gyData.get("qxsd"),gyData.get("mxbj")));
            simulationData.put("jgsdY", calcJjsd(gyData.get("jjlY"),gyData.get("qxsd"),gyData.get("mxbj")));
            simulationDataList.add(simulationData);
        }
        return simulationDataList;
    }
    /**
     * 计算仿真的单个单元删除时长
     * @param gjbj
     * @param qxsd
     * @return
     */
    private static BigDecimal calcScsj(Object gjbj, Object qxsd){
        BigDecimal qxsdValue;
        BigDecimal gjbjValue;
        try{
            qxsdValue=new BigDecimal(qxsd.toString());
            gjbjValue=new BigDecimal(gjbj.toString());
        }catch (Exception e){
            return BigDecimal.ZERO;
        }
        if(qxsdValue.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }
        //公式=工件半径*3.14/单元格数量/60/切削速度
        return gjbjValue.multiply(new BigDecimal("3.14")).divide(new BigDecimal("100")).divide(new BigDecimal("60"),10, RoundingMode.HALF_UP).divide(qxsdValue,2, RoundingMode.HALF_UP);
    }

    private static BigDecimal calcJsd(Object qxsd, Object mxbj){
        BigDecimal qxsdValue;
        BigDecimal mxbjValue;
        try{
            qxsdValue=new BigDecimal(qxsd.toString());
            mxbjValue=new BigDecimal(mxbj.toString());
        }catch (Exception e){
            return BigDecimal.ZERO;
        }
        if(qxsdValue.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }
        return qxsdValue.multiply(new BigDecimal("1000")).divide(new BigDecimal("60"),10, RoundingMode.HALF_UP).divide(mxbjValue,2, RoundingMode.HALF_UP);
    }

    private static BigDecimal calcJjsd(Object jjl, Object qxsd, Object gjbj){
        BigDecimal qxsdValue;
        BigDecimal gjbjValue;
        BigDecimal jjlValue;
        try{
            qxsdValue=new BigDecimal(qxsd.toString());
            gjbjValue=new BigDecimal(gjbj.toString());
            jjl = new BigDecimal(jjl.toString());
        }catch (Exception e){
            return BigDecimal.ZERO;
        }
        BigDecimal multiValue = gjbjValue.multiply(qxsdValue);
        if(multiValue.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }
        return multiValue.multiply(new BigDecimal("1000")).divide(new BigDecimal("60"),10, RoundingMode.HALF_UP).divide(gjbjValue,2, RoundingMode.HALF_UP);
    }

}
